﻿/******************************************************************************
* File description: FBNPushButton.qml
* Function: 放靶按钮控件
* Current Version: 1.1
* Author: 陈乃瑞
* Date: 2025-07-01
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.12
import QtGraphicalEffects 1.0
import QtQuick.Window 2.12
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.2
import QtQuick.Controls 2.12
import QtMultimedia 5.14
Item {


    id: root

    property string title: ""

    // 按钮样式
    property string button_icon: ""
    property string textval: ""
    property string numval: ""
    property string button_color: "white"
    property string text_color: "#333333"
    property string text_size: config.fontsize
    property string button_border: "transparent"
    property string icon_color: ""
    property var icon_width: idImage.parent.width
    property var icon_height: idImage.parent.height

    // 选中时样式
    property string button_icon_select: button_icon
    property string button_color_select: button_color
    property string text_color_select: text_color
    property string button_border_select: button_border
    property string icon_color_select: icon_color

    // Hover时样式
    property string button_icon_hover: button_icon
    property string button_color_hover: button_color
    property string text_color_hover: text_color
    property string button_border_hover: button_border
    property string icon_color_hover: icon_color

    // 点击时样式
    property string button_icon_press: button_icon
    property string button_color_press: button_color
    property string text_color_press: text_color
    property string button_border_press: button_border
    property string icon_color_press: icon_color

    // 禁用时样式
    property string button_icon_disable: button_icon
    property string button_color_disable: "#F5F5F5"
    property string text_color_disable: "#BFBFBF"
    property string button_border_disable: "#CCCCCC"
    property string icon_color_disable: "#CCCCCC"


    property real radius: 3*dp

    property bool isSelected: false
    property bool isDisabled: false
    property bool isPressed: false
    property bool isHover: false
    property bool textvalflag: false

    // 预设方案
    property bool underlineSelect: false    // 下面带蓝杠的可选中按钮
    property bool defaultMode1: false       // 蓝色按钮（在onCompleted中设置）
    property bool defaultMode2: false       // 白色按钮（在onCompleted中设置）



    // 选中样式
    function setSelect(){
        isSelected = true
    }
    function setUnselect(){
        isSelected = false
    }
    signal clickccc(string titleval,string numval,string textval)

    // 自定义菜单（使用 Popup 或 Rectangle + Text 模拟）
    Popup {
        id: menu
        visible: false
        width: 200*dp
        height: 120*dp
        background: Rectangle
//        overlay: true

        ColumnLayout {
            anchors.fill: parent
            spacing: 1*dp

            PushButton {
                width: parent.width
                height: 30*dp
                button_icon: "qrc:/picture/pz_db2.png"
                icon_width: 24*dp
                icon_height: 24*dp
                button_color: "#00417E"
                icon_color: "white"
                text_color: "white"
                title: "添加挡板"
                onReleased: {
                    // 添加逻辑
                    menu.visible = false
                    // 例如，添加一个新的 NPushButton 到某个父容器
                    CExeConfig.fbaddkj(syid,onnb,"1")
                    buttonData=CExeConfig.fbreadsypz(syid)
                }
            }
            PushButton {
                width: parent.width
                height: 30*dp
                button_icon: "qrc:/picture/pz_quba2.png"
                icon_width: 24*dp
                icon_height: 24*dp
                button_color: "#5BA58C"
                icon_color: "white"
                text_color: "white"
                title: "添加取放靶"
                onReleased: {
                    // 添加逻辑
                    menu.visible = false
                    // 例如，添加一个新的 NPushButton 到某个父容器
                    CExeConfig.fbaddkj(syid,onnb,"2")
                    buttonData=CExeConfig.fbreadsypz(syid)
                }
            }
            PushButton {
                width: parent.width
                height: 30*dp
                button_icon: "qrc:/picture/pz_jz2.png"
                icon_width: 24*dp
                icon_height: 24*dp
                button_color: "#705BA5"
                icon_color: "white"
                text_color: "white"
                title: "添加夹爪"
                onReleased: {
                    // 添加逻辑
                    menu.visible = false
                    // 例如，添加一个新的 NPushButton 到某个父容器
                    CExeConfig.fbaddkj(syid,onnb,"3")
                    buttonData=CExeConfig.fbreadsypz(syid)
                }
            }
            PushButton {
                width: parent.width
                height: 30*dp
                button_icon: "qrc:/picture/pz_cb2.png"
                icon_width: 24*dp
                icon_height: 24*dp
                button_color: "#89439B"
                icon_color: "white"
                text_color: "white"
                title: "添加热室端送靶"
                onReleased:  {
                    // 添加逻辑
                    menu.visible = false
                    // 例如，添加一个新的 NPushButton 到某个父容器
                    CExeConfig.fbaddkj(syid,onnb,"5")
                    buttonData=CExeConfig.fbreadsypz(syid)
                }
            }
            PushButton {
                width: parent.width
                height: 30*dp
                button_icon: "qrc:/picture/pz_dd.png"
                icon_width: 24*dp
                icon_height: 24*dp
                button_color: "#FFB1D8"
                text_color: "white"
                title: "添加等待"
                onReleased: {
                    // 添加逻辑
                    menu.visible = false
                    // 例如，添加一个新的 NPushButton 到某个父容器
                    CExeConfig.fbaddkj(syid,onnb,"7")
                    buttonData=CExeConfig.fbreadsypz(syid)
                }
            }
            PushButton {
                width: parent.width
                height: 30*dp
                button_icon: "qrc:/picture/pz_zt.png"
                icon_width: 24*dp
                icon_height: 24*dp
                button_color: "#89B5FF"
                text_color: "white"
                title: "添加暂停"
                onReleased: {
                    // 添加逻辑
                    menu.visible = false
                    // 例如，添加一个新的 NPushButton 到某个父容器
                    CExeConfig.fbaddkj(syid,onnb,"8")
                    buttonData=CExeConfig.fbreadsypz(syid)
                }
            }
            PushButton {
                id:sc
                width: parent.width
                height: 30*dp
                button_color: "#FF9A3F"
                text_color: "white"
                title: "删除"
                onReleased: {
                    // 删除逻辑
                    menu.visible = false
                    // 例如，从父容器中删除这个 NPushButton
                    CExeConfig.fbdelkj(syid,onnb)
                    buttonData=CExeConfig.fbreadsypz(syid)
                }
            }
        }
    }

    // 预设方案设置
    Component.onCompleted: {
        if(defaultMode1){       // 蓝色按钮
            button_color = "#1677FF"
            text_color   = "white"
            button_border = "transparent"
            button_color_press = "#1162F3"

        }else if(defaultMode2){     // 白色按钮
            button_color = "white"
            text_color   = "#333333"
            button_border = "#CCCCCC"
            button_color_press = "#F7FAFE"
        }
    }


    width: 120*dp
    height: 40*dp

    signal released(var mouseX, var mouseY)
    signal pressed(var mouseX, var mouseY)
    signal positionChanged(var mouseX,var mouseY)
    signal released_disabled()

    // 按钮背景
    Rectangle{
        id: idRectangle
        anchors.fill: parent
        color: {
            if(isDisabled){
                return button_color_disable
            }else if(isSelected){
                return button_color_select
            }else if(isPressed){
                return button_color_press
            }else if(isHover){
                return button_color_hover
            }else{
                return button_color
            }
        }
        border.color: {
            if(isDisabled){
                return button_border_disable
            }else if(isSelected){
                return button_border_select
            }else if(isPressed){
                return button_border_press
            }else if(isHover){
                return button_border_hover
            }else{
                return button_border
            }
        }
        radius: underlineSelect ? 0 : root.radius
    }
    // 按钮图标
    Image {
        id: idImage
        height: icon_height
        width: icon_width
        x: 10*dp+20*dp
        y: (parent.height-icon_height)/2
        source: {
            if(isDisabled){
                return button_icon_disable
            }else if(isSelected){
                return button_icon_select
            }else if(isPressed){
                return button_icon_press
            }else if(isHover){
                return button_icon_hover
            }else{
                return button_icon
            }
        }
        mipmap: true
        ColorOverlay{
            visible: icon_color!==""
            anchors.fill: parent
            source: parent
            color: {
                if(isDisabled){
                    return icon_color_disable
                }else if(isSelected){
                    return icon_color_select
                }else if(isPressed){
                    return icon_color_press
                }else if(isHover){
                    return icon_color_hover
                }else{
                    return icon_color
                }
            }
        }
    }
    // 按钮图标2
    Rectangle {
        id: idImage2
        height: 20*dp
        width: 25*dp
        visible: textvalflag
        x: parent.width-30*dp
        y: (parent.height-20*dp)/2
        // 定义一个JavaScript函数来判断并返回颜色值
        function getColorBasedOnText() {
            if (title === "注射器") {
                return "#B3B7FF";
            }
            return "transparent";
        }
        color: getColorBasedOnText()
        Text {
            text: title === "注射器"?textval:""
            anchors.centerIn: parent // 让文字在该矩形中居中
        }
    }
    Rectangle{
        x:10*dp
        y:(parent.height-20*dp)/2
        width: 30*dp
        height: 32*dp
        color: "transparent"
        Text {
            id: numb
            text: numval
            color: "white"
            font.bold: true
        }
    }

    // 按钮文字
    Text{
        id: idText
        font.pixelSize: text_size
        x:idImage.width+15*dp+20*dp
        y:(parent.height-idText.height)/2
        text: title
        color: {
            if(isDisabled){
                return text_color_disable
            }else if(isSelected){
                return text_color_select
            }else if(isPressed){
                return text_color_press
            }else if(isHover){
                return text_color_hover
            }else{
                return text_color
            }
        }
    }


    MouseArea{
        anchors.fill: parent
        cursorShape: Qt.PointingHandCursor
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        hoverEnabled: true
        onEntered: {
            isHover = true
        }onExited: {
            isHover = false
        }onPositionChanged: {
            root.positionChanged(mouseX,mouseY)
        }

        onPressed: {
            console.log(onnb)
            isPressed = true
            if(title!=="开始"&&title!=="结束"){
                clickccc(title,numval,textval)
            }else if(title==="开始"){
                onnb=0
            }

            isupda=false
//            root.pressed(mouseX,mouseY)
        }
        onReleased: {
            isPressed = false
            if(mouse.button === Qt.RightButton){
                if(title!=="结束"){
                    if(title==="开始"){
                        sc.visible=false
                    }else{
                        sc.visible=true
                    }
                    // 显示自定义菜单的逻辑
                    menu.visible = true
                    menu.x = mouseX
                    menu.y = mouseY-200*dp // 将菜单放在按钮下方
                }
            }
        }
    }

    CustomBorder{
        id: underline
        bBorderwidth: 2*dp
        borderColor: "#1677FF"
        visible: underlineSelect && isSelected
    }

}
